﻿using Cysharp.Threading.Tasks;
using Net.Share;
using Wordgames;
using WordGames.System;

namespace WordGames
{
    internal class Login : IProcedure
    {
        public async UniTask Execute(TcpNetwork client)
        {
            Console.WriteLine("1.注册账号:r 123 456");
            Console.WriteLine("2.登录账号:l 123 456");
            var command = Console.ReadLine();
            if (command.StartsWith("r"))
            {
                var args = command.Split(' ');
                var code = await client.Request<int>(ProtoType.Register, RequestTimeout.To(1000 * 30), args[1], args[2]);
                Console.WriteLine(code == 1 ? "注册成功!" : code == -1 ? "注册失败!" : "请求超时!");
            }
            if (command.StartsWith("l"))
            {
                var args = command.Split(' ');
                var (code, user) = await client.Request<int, UserData>(ProtoType.Login, RequestTimeout.To(1000 * 30), args[1], args[2]);
                await UniTask.Yield(); //切换
                Console.WriteLine(code == 1 ? "登录成功!" : code == -1 | code == -2 ? "登录失败!" : "请求超时!");
                if (code == 1)
                {
                    Global.User = user;
                    ProcedureManager.Instance.procedure = new Main(client);
                }
                else return;
                Console.WriteLine("回车进入主界面");
                Console.ReadLine();
                await client.Request<int>(ProtoType.PlayerEnterGame, RequestTimeout.To(1000 * 30));
                if (string.IsNullOrEmpty(user.Name))
                {
                    Console.Write("玩家名称:");
                    command = Console.ReadLine();
                    await client.Request<int>(ProtoType.SetPlayerName, RequestTimeout.To(1000 * 30), command);
                }
            }
        }

        public async UniTask Execute(List<TcpNetwork> clients)
        {
            Console.WriteLine("1.注册账号:r");
            Console.WriteLine("2.登录账号:l");
            var command = Console.ReadLine();
            if (command.StartsWith("r"))
            {
                var tasks = new UniTask<int>[clients.Count];
                for (int i = 0; i < clients.Count; i++)
                {
                    var client = clients[i];
                    var account = (i + 1).ToString();
                    var password = (i + 1).ToString();
                    tasks[i] = client.Request<int>(ProtoType.Register, RequestTimeout.To(1000 * 30), account, password);
                }
                var result = await UniTask.WhenAll(tasks);
                var success = 0;
                var fail = 0;
                var timeout = 0;
                for (int i = 0; i < result.Length; i++)
                {
                    if (result[i] == 1)
                        success++;
                    else if (result[i] == -1)
                        fail++;
                    else
                        timeout++;
                }
                Console.WriteLine($"注册成功:{success} 注册失败:{fail} 请求超时:{timeout}");
            }
            if (command.StartsWith("l"))
            {
                var tasks = new UniTask<ValueTuple<int, UserData>>[clients.Count];
                for (int i = 0; i < clients.Count; i++)
                {
                    var client = clients[i];
                    var account = (i + 1).ToString();
                    var password = (i + 1).ToString();
                    tasks[i] = client.Request<int, UserData>(ProtoType.Login, RequestTimeout.To(1000 * 30), account, password);
                }
                var result = await UniTask.WhenAll(tasks);
                var success = 0;
                var fail = 0;
                var timeout = 0;
                for (int i = 0; i < result.Length; i++)
                {
                    if (result[i].Item1 == 1)
                    {
                        clients[i].User = result[i].Item2;
                        success++;
                    }
                    else if (result[i].Item1 == -1 | result[i].Item1 == -2)
                        fail++;
                    else
                        timeout++;
                }
                Console.WriteLine($"登录成功:{success} 登录失败:{fail} 请求超时:{timeout}");
                Console.WriteLine("回车进入主界面");
                Console.ReadLine();
                ProcedureManager.Instance.procedure = new Main(clients);
                var tasks1 = new UniTask<int>[clients.Count];
                for (int i = 0; i < clients.Count; i++)
                {
                    var client = clients[i];
                    tasks1[i] = client.Request<int>(ProtoType.PlayerEnterGame, RequestTimeout.To(1000 * 30));
                }
                var result1 = await UniTask.WhenAll(tasks1);
                success = 0;
                timeout = 0;
                for (int i = 0; i < result1.Length; i++)
                {
                    if (result1[i] == 1)
                        success++;
                    else
                        timeout++;
                }
                Console.WriteLine($"进入游戏成功:{success} 请求超时:{timeout}");
                var tasks2 = new UniTask<int>[clients.Count];
                for (int i = 0; i < clients.Count; i++)
                {
                    var client = clients[i];
                    client.Name = (i + 1).ToString();
                    tasks2[i] = client.Request<int>(ProtoType.SetPlayerName, RequestTimeout.To(1000 * 30), (i + 1).ToString());
                }
                var result2 = await UniTask.WhenAll(tasks2);
                success = 0;
                timeout = 0;
                for (int i = 0; i < result2.Length; i++)
                {
                    if (result2[i] == 1)
                        success++;
                    else
                        timeout++;
                }
                Console.WriteLine($"设置玩家名称成功:{success} 请求超时:{timeout}");
            }
        }
    }
}